<?php
//创建WebSocket Server对象，监听0.0.0.0:9502端口
$ws = new Swoole\WebSocket\Server('0.0.0.0', 6065);
$ws->set([
    'enable_static_handler' => true,
    'document_root' => '/wwwdata/webSocket', // v4.4.0以下版本, 此处必须为绝对路径
]);
//监听WebSocket连接打开事件
$ws->on('Open','onOpen');
function onOpen($ws,$request)
{
    print_r($request->fd);
}
//监听WebSocket消息事件
$ws->on('Message', function ($ws, $frame) {
//    echo "Message: {$frame->data}\n";
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $ws->push($frame->fd, "server: Qin-Qin-push-success->{$frame->data}");
});

//监听WebSocket连接关闭事件
$ws->on('Close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();
